package com.c.yinyuezhushou.Sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.c.yinyuezhushou.Entity.SongListEntity;
import com.c.yinyuezhushou.MRetrofit.MusicItem;
import com.c.yinyuezhushou.MRetrofit.getSongDetail;
import com.c.yinyuezhushou.MyApplication;

import java.util.ArrayList;
import java.util.List;

public class MyDButil {
    private  MyApplication myApplication;
    private SQLiteDatabase sqLiteDatabase;
    public MyDButil(MyApplication myApplication , SQLiteDatabase sqLiteDatabase){
        this.myApplication = myApplication;
        this.sqLiteDatabase = sqLiteDatabase;
    }


    public boolean setMusicItemTable(MusicItem musicItem, String tname){
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",musicItem.getId());
        contentValues.put("song",musicItem.getSong());
        contentValues.put("singer",musicItem.getSinger());
        contentValues.put("imageurl",musicItem.getImageurl());
        if (sqLiteDatabase.insert(tname,null,contentValues)>0){
            return true;
        }
        return false;
    }

    public boolean setMusicItemTable(ContentValues contentValues, String tname){
        if (sqLiteDatabase.insert(tname,null,contentValues)>0){
            return true;
        }
        return false;
    }

    public boolean removeMusicItemTable(MusicItem musicItem, String tname){
        if (sqLiteDatabase.delete(tname,"id = ?",new String[]{String.valueOf(musicItem.getId())})>0){
            return true;
        }
        return false;
    }

    public boolean isMusicItemTable(MusicItem musicItem, String tname){
        Cursor cursor = sqLiteDatabase.rawQuery("select * from "+tname+" where id = ?",new String[]{String.valueOf(musicItem.getId())});
        if (cursor.moveToFirst()){
            return true;
        }
        return false;
    }

    public List<MusicItem> getMusicItemTable(String tname){
        Cursor cursor = sqLiteDatabase.rawQuery("select * from "+tname,null);
        List<MusicItem> list = new ArrayList<MusicItem>();
        if (cursor.moveToFirst()){
            do {
                MusicItem musicItem1 = new MusicItem();
                musicItem1.setId(cursor.getInt(0));
                musicItem1.setSong(cursor.getString(1));
                musicItem1.setSinger(cursor.getString(2));
                musicItem1.setImageurl(cursor.getString(3));
                list.add(musicItem1);
            }while (cursor.moveToNext());
        }
        return list;
    }

    public List<SongListEntity> getSongList(){
         Cursor cursor = sqLiteDatabase.rawQuery("select * from SongList",null);
        List<SongListEntity> list = new ArrayList<SongListEntity>();
        if (cursor.moveToFirst()){
            do {
                SongListEntity songListEntity = new SongListEntity();
                songListEntity.setImageurl(cursor.getString(0));
                songListEntity.setListname(cursor.getString(1));
                list.add(songListEntity);
            }while (cursor.moveToNext());
        }
        return list;
    }

    public List<MusicItem> getSongListMusicItem(String slname){
        Cursor cursor = sqLiteDatabase.rawQuery("select * from SongListMusicItem where slname = ?",new String[]{slname});
        List<MusicItem> musicItems = new ArrayList<MusicItem>();
        if (cursor.moveToFirst()){
            do {
                MusicItem musicItem = new MusicItem();
                musicItem.setId(cursor.getInt(0));
                musicItem.setSong(cursor.getString(1));
                musicItem.setSinger(cursor.getString(2));
                musicItem.setImageurl(cursor.getString(3));
                musicItems.add(musicItem);
            }while (cursor.moveToNext());
        }
        return musicItems;
    }

    public boolean removeSongList(String name){
        if (sqLiteDatabase.delete("SongList","slname = ?",new String[]{name})>0){
            sqLiteDatabase.delete("SongListMusicItem","slname = ?",new String[]{name});
            return true;
        }
        return false;
    }

    public void setSongListMusicItem(getSongDetail.SongsBean songDetail,String name){
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",songDetail.getId());
        contentValues.put("song",songDetail.getName());
        contentValues.put("singer",songDetail.getAr().get(0).getName());
        contentValues.put("imageurl",songDetail.getAl().getPicUrl()+"?imageView=1&thumbnail=400z400&type=webp&quality=80");
        contentValues.put("slname",name);
        this.setMusicItemTable(contentValues,"SongListMusicItem");
    }

    public void setSongListMusicItem(MusicItem musicItem,String name){
        ContentValues contentValues = new ContentValues();
        contentValues.put("id",musicItem.getId());
        contentValues.put("song",musicItem.getSong());
        contentValues.put("singer",musicItem.getSinger());
        contentValues.put("imageurl",musicItem.getImageurl());
        contentValues.put("slname",name);
        this.setMusicItemTable(contentValues,"SongListMusicItem");
    }

    public boolean setSongList(String name,String url){
        ContentValues contentValues = new ContentValues();
        contentValues.put("imageurl",url);
        contentValues.put("slname",name);
        long i = sqLiteDatabase.insert("SongList",null,contentValues);
        return i>0;
    }

    public boolean isSongList(String name){
        Cursor cursor = sqLiteDatabase.rawQuery("select * from SongListMusicItem where slname = ?",new String[]{name});
        if (cursor.moveToFirst()){
            return true;
        }
        return false;
    }


}
